package com.xw.childhealth.service.impl;

import com.xw.childhealth.dao.NeonatalVisitDao;
import com.xw.childhealth.entity.NeonatalVisit;
import com.xw.childhealth.service.NeonatalVisitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: y
 * @CreateDate: 2019-12-03 14:21
 * @Version: 1.0
 */
@Service
public class NeonatalVisitServiceImpl implements NeonatalVisitService {

    @Autowired
    private NeonatalVisitDao dao;


    @Override
    public Page<NeonatalVisit> findAll(Integer page, Integer size, HttpServletRequest request) {
        Specification<NeonatalVisit> specification = new Specification<NeonatalVisit>() {
            @Override
            public Predicate toPredicate(Root<NeonatalVisit> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {

                List<Predicate> list = new ArrayList<Predicate>();

                String searchUserName = request.getParameter("searchUserName");
                if (!StringUtils.isEmpty(searchUserName)) {
                    Predicate p1 = criteriaBuilder.like(root.get("xm").as(String.class), "%" + searchUserName.trim() + "%");
                    list.add(p1);
                }
                String searchMatherName = request.getParameter("searchMatherName");
                if (!StringUtils.isEmpty(searchMatherName)) {
                    Predicate p2 = criteriaBuilder.like(root.get("mqxm").as(String.class), "%" + searchMatherName.trim() + "%");
                    list.add(p2);
                }

                String start = request.getParameter("start");
                String end = request.getParameter("end");
                if (!StringUtils.isEmpty(start) && !StringUtils.isEmpty(end)) {
                    Predicate p3 = criteriaBuilder.between(root.get("csrq").as(String.class), start, end);
                    list.add(p3);
                } else if (!StringUtils.isEmpty(start)) {
                    Predicate p4 = criteriaBuilder.between(root.get("csrq").as(String.class), start, "2222-02-02");
                    list.add(p4);
                }

                return criteriaBuilder.and(list.toArray(new Predicate[0]));
            }
        };

        Sort sort = Sort.by(Sort.Order.desc("createTime"));
        PageRequest pageRequest = PageRequest.of(page < 0 ? 0 : page, size, sort);

        Page<NeonatalVisit> all = dao.findAll(specification, pageRequest);

        return all;
    }

    @Override
    public NeonatalVisit save(NeonatalVisit neonatalVisit) {
        return dao.save(neonatalVisit);
    }

    @Override
    public NeonatalVisit findOneById(Integer id) {
        return dao.findById(id).orElse(null);
    }

    @Override
    public void delById(Integer id) {
        dao.deleteById(id);
    }
}
